package com.ozo.easy.common.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.thymeleaf.dialect.SaTokenDialect;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * sa-token配置类
 *
 * @author ozo
 * @date 2024-12-26
 */
@Slf4j
@RequiredArgsConstructor
@Configuration
@EnableConfigurationProperties(OzoProperties.class)
public class SaTokenConfiguration implements WebMvcConfigurer {

    private final OzoProperties ozoProperties;

    /**
     * 注册Sa-Token拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验
        registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
                .addPathPatterns("/**")
                .excludePathPatterns(ozoProperties.getWhitePaths());
    }

    /**
     * Sa-Token 标签方言 (Thymeleaf版)
     */
    @Bean
    public SaTokenDialect saTokenDialect() {
        return new SaTokenDialect();
    }

}
